package carltechnology.com.mahjonggame.views.impl;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.ButterKnife;
import butterknife.InjectView;
import carltechnology.com.mahjonggame.R;
import carltechnology.com.mahjonggame.mode.event.FragmentViewEvent;
import carltechnology.com.mahjonggame.utils.Utils;
import carltechnology.com.mahjonggame.views.IMahjongGameInputView;
import carltechnology.com.mahjonggame.views.IMahjongGameView;
import carltechnology.com.mahjonggame.views.IMainActivity;

/**
 * Created by WZTCM on 2016/6/5.
 */
public class MahjongGameActivity extends AppCompatActivity implements IMainActivity {

    private IMahjongGameView iMahjongGameView;
    private IMahjongGameInputView iMahjongGameInputView;
//    private FragmentManager fragmentManager;
    @InjectView(R.id.mahjonggame_fab)
    FloatingActionButton floatingActionButton;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mahjonggame);
        ButterKnife.inject(this);
       // fragmentManager = getFragmentManager();
        Utils.getEventBus().register(this);
        Utils.getEventBus().post(new FragmentViewEvent(FragmentViewEvent.EVENT.EVENT_MAHJONG_MAIN_FRAGMENT));
        floatingActionButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toInputView();
            }
        });
        iMahjongGameView = new MahjongGameFragment();
        iMahjongGameInputView = new MahjonggameInputFragment();
    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(FragmentViewEvent event){
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        switch (event.getEvent()){
            case EVENT_MAHJONG_INPUT_FRAGMENT:
                transaction.replace(R.id.mahjonggame_content_framgment,new MahjonggameInputFragment());
                break;
            case EVENT_MAHJONG_MAIN_FRAGMENT:
                transaction.replace(R.id.mahjonggame_content_framgment,new MahjongGameFragment());
                break;
            case EVENT_MAHJONG_DETAIL_FRAGMENT:
                transaction.replace(R.id.mahjonggame_content_framgment,new MahjongGameDetailFragment());
        }

        transaction.commit();
       //

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Utils.getEventBus().unregister(this);
    }

    @Override
    public void toInputView() {
        Utils.getEventBus().post(new FragmentViewEvent(FragmentViewEvent.EVENT.EVENT_MAHJONG_INPUT_FRAGMENT));
    }
}
